package net.javashop.util.encryption;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptUtil {
	public static boolean checkPassword(String encryptString, String paramString2) {
		boolean bool = false;
		try {
			BASE64Decoder localBASE64Decoder = new BASE64Decoder();
			byte[] arrayOfByte1 = localBASE64Decoder.decodeBuffer(encryptString);
			byte[] arrayOfByte2 = new byte[12];
			System.arraycopy(arrayOfByte1, 0, arrayOfByte2, 0, 12);
			MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
			localMessageDigest.update(arrayOfByte2);
			localMessageDigest.update(paramString2.getBytes("UTF8"));
			byte[] arrayOfByte3 = localMessageDigest.digest();
			byte[] arrayOfByte4 = new byte[arrayOfByte1.length - 12];
			System.arraycopy(arrayOfByte1, 12, arrayOfByte4, 0,
					arrayOfByte1.length - 12);
			if (Arrays.equals(arrayOfByte3, arrayOfByte4))
				bool = true;
			else
				bool = false;
		} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
			bool = encryptString.equals(paramString2);
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
			bool = encryptString.equals(paramString2);
		} catch (IOException e) {
			bool = encryptString.equals(paramString2);
		}
		return bool;
	}

	public static String encodePassword(String paramString) {
		String str = null;
		try {
			SecureRandom localSecureRandom = new SecureRandom();
			byte[] arrayOfByte1 = new byte[12];
			localSecureRandom.nextBytes(arrayOfByte1);
			MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
			localMessageDigest.update(arrayOfByte1);
			localMessageDigest.update(paramString.getBytes("UTF8"));
			byte[] arrayOfByte2 = localMessageDigest.digest();
			byte[] arrayOfByte3 = new byte[arrayOfByte1.length
					+ arrayOfByte2.length];
			System.arraycopy(arrayOfByte1, 0, arrayOfByte3, 0,
					arrayOfByte1.length);
			System.arraycopy(arrayOfByte2, 0, arrayOfByte3,
					arrayOfByte1.length, arrayOfByte2.length);
			BASE64Encoder localBASE64Encoder = new BASE64Encoder();
			str = localBASE64Encoder.encode(arrayOfByte3);
		} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
			str = paramString;
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
			str = paramString;
		}
		return str;
	}

	public static String encodeMD5(String paramString) {
		return encodePassword(paramString);
	}

	public static void main(String[] paramArrayOfString) {
		String str = encodePassword("admin");
		System.out.println("pwd = " + str);
		boolean bool = checkPassword(str, "admin");
		System.out.println("good = " + ((bool) ? "yes" : "no"));
	}
}